package com.zheng.shop.rpc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.shop.common.exception.ShopException;
import com.zheng.shop.dao.mapper.ShopCategoryMapper;
import com.zheng.shop.dao.model.ShopAttributeKey;
import com.zheng.shop.dao.model.ShopCategory;
import com.zheng.shop.dao.model.ShopParam;
import com.zheng.shop.rpc.api.ShopAttributeKeyService;
import com.zheng.shop.rpc.api.ShopCategoryService;
import com.zheng.shop.rpc.api.ShopParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author likun
 * @since 2017-12-29
 */
@Service
@Transactional(rollbackFor = ShopException.class)
@BaseService
public class ShopCategoryServiceImpl extends BaseServiceImpl<ShopCategoryMapper, ShopCategory> implements ShopCategoryService {
    private static Logger _log = LoggerFactory.getLogger(ShopCategoryServiceImpl.class);

    @Autowired
    ShopCategoryMapper categoryMapper;
    @Autowired
    ShopAttributeKeyService keyService;
    @Autowired
    ShopParamService paramService;

    @Override
    public Integer selectMaxSort() {
        return categoryMapper.selectMaxSort();
    }

    @Override
    public boolean deleteProductCategory(int id) {
        EntityWrapper<ShopCategory> categoryEntityWrapper = new EntityWrapper<>();
        categoryEntityWrapper.where("pid=" + id);
        ShopCategory category = super.selectOne(categoryEntityWrapper);
        //如果有子分类，就删除失败
        if (null != category) {
            return false;
        }
        return super.deleteById(id);
    }

    public boolean deleteBatchCategory(List<Integer> ids) throws ShopException {
        for (Integer id : ids) {
            if (!deleteProductCategory(id)) {
                throw new ShopException("删除的分类下有未删除的子分类！");
            }
            EntityWrapper<ShopAttributeKey> attributeKeyEntityWrapper = new EntityWrapper<>();
            attributeKeyEntityWrapper.where("category_id=" + id);
            List<ShopAttributeKey> shopProductAttributeKeyList = keyService.selectList(attributeKeyEntityWrapper);
            List<Integer> keyIdList = new ArrayList<>();
            for (ShopAttributeKey key : shopProductAttributeKeyList) {
                keyIdList.add(key.getId());
            }
            keyService.deleteProductAttribute(keyIdList);

            EntityWrapper<ShopParam> paramEntityWrapper = new EntityWrapper<>();
            paramEntityWrapper.where("category_id=" + id);
            paramService.delete(paramEntityWrapper);
        }
        return true;
    }

    public boolean createProductCategory(ShopCategory productCategory, boolean copyAttribute, boolean copyParam) throws ShopException {
        productCategory.setCtime(new Date());
        Integer sort = selectMaxSort() + 1;
        productCategory.setSort(sort);
        if (!insert(productCategory)) {
            throw new ShopException("新增商品分类失败！");
        }
        //添加商品分类后修改该分类的path
        if (!updatePath(productCategory)) {
            throw new ShopException("新增商品分类失败！");
        }
        //查找path中的所有父分类属性，并复制
        if (copyAttribute) {
            if (productCategory.getPid() != 0) {
                Integer id = productCategory.getId();
                Integer pId = productCategory.getPid();

                EntityWrapper<ShopAttributeKey> attributeKeyEntityWrapper = new EntityWrapper<>();
                attributeKeyEntityWrapper.where("pid=" + pId);
                List<ShopAttributeKey> shopProductAttributeKeys = keyService.selectList(attributeKeyEntityWrapper);
                List<Integer> keyIdList = new ArrayList<>();
                for (ShopAttributeKey key : shopProductAttributeKeys) {
                    keyIdList.add(key.getId());
                }
                keyService.copyProductAttribute(keyIdList, id);
            }
        }
        if (copyParam) {
            EntityWrapper<ShopParam> paramEntityWrapper = new EntityWrapper<>();
            paramEntityWrapper.where("category_id=" + productCategory.getId());
            List<ShopParam> paramList = paramService.selectList(paramEntityWrapper);

            for (ShopParam param : paramList) {
                param.setId(null);
                param.setCategoryId(productCategory.getId());
                if (!paramService.insert(param)) {
                    throw new ShopException("复制参数失败！");
                }
            }
        }
        return true;
    }

    //修改path
    private boolean updatePath(ShopCategory productCategory) throws ShopException {
        ShopCategory pProductCategory = selectById(productCategory.getPid());
        String path;
        if (null != pProductCategory) {
            if (pProductCategory.getPath().contains(productCategory.getId().toString())) {
                throw new ShopException("请选择正确的父分类");
            }
            path = pProductCategory.getPath() + productCategory.getId() + "_";
        } else {
            path = "0_" + productCategory.getId() + "_";
        }
        productCategory.setPath(path);
        return updateById(productCategory);
    }

    public boolean updateProductCategory(ShopCategory productCategory) throws ShopException {
        productCategory.setMtime(new Date());
        return updatePath(productCategory);
    }
}
